﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using JXC.Core.Models;
using JXC.EFCore;
using AutoMapper;
using JXC.Application.Dtos;

namespace JXC.WebAPI.Controllers
{
    /// <summary>
    /// 采购订单明细
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class CaiGouOrderDetailsController : ControllerBase
    {
        private readonly AppDbContext _context;
        private readonly IMapper _mapper;

        public CaiGouOrderDetailsController(

            AppDbContext context,
            IMapper mapper

            )
        {
            _context = context;
            _mapper = mapper;

        }

        /// <summary>
        /// 获取多个采购订单的订单明细
        /// </summary>
        /// <returns></returns>
        // GET: api/CaiGouOrderDetails

        [Route("GetData")]
        [HttpPost]
        public async Task<ActionResult<IEnumerable<CaigouOrderDetail>>> GetCaiGouOrderDetails([FromBody] List<Guid> guids)
        {
            List<CaiGouOrderDetail> caiGouOrderDetails = new List<CaiGouOrderDetail>();
            foreach (var item in guids)
            {
                var res = await _context.CaiGouOrderDetails.Include(p => p.CommodityInformationSheet).Include(p => p.CaigouOrder).Where(p => p.CaigouOrderId == item).ToListAsync();

                caiGouOrderDetails.AddRange(res);
            }
            var res2 = _mapper.Map<List<CaigouOrderDetail>>(caiGouOrderDetails);
            return res2;
        }
       
        //[HttpGet("{id}")]
        [HttpGet]
        public async Task<ActionResult<CaigouOrderDetail>> GetCaiGouOrderDetails([FromBody] Guid id)
        {
            await _context.Alarm.ToListAsync();
            return Ok();
        }

        // GET: api/CaiGouOrderDetails/5
        [HttpGet("{id}")]
        public async Task<ActionResult<List< CaiGouOrderDetail >>> GetCaiGouOrderDetail(Guid id)
        {
            var res = await _context.CaiGouOrderDetails.Include(p => p.CommodityInformationSheet).Include(p => p.CaigouOrder).Where(p => p.CaigouOrderId == id).ToListAsync();


            //var res2 = _mapper.Map<CaigouOrderDetailDto>(res);
            return res;
        }

        // PUT: api/CaiGouOrderDetails/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public async Task<IActionResult> PutCaiGouOrderDetail(Guid id, CaiGouOrderDetail caiGouOrderDetail)
        {
            if (id != caiGouOrderDetail.Id)
            {
                return BadRequest();
            }

            _context.Entry(caiGouOrderDetail).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CaiGouOrderDetailExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/CaiGouOrderDetails
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<CaiGouOrderDetail>> PostCaiGouOrderDetail(CaiGouOrderDetail caiGouOrderDetail)
        {
            _context.CaiGouOrderDetails.Add(caiGouOrderDetail);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetCaiGouOrderDetail", new { id = caiGouOrderDetail.Id }, caiGouOrderDetail);
        }

        // DELETE: api/CaiGouOrderDetails/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteCaiGouOrderDetail(string id)
        {
            var caiGouOrderDetail = await _context.CaiGouOrderDetails.FindAsync(id);
            if (caiGouOrderDetail == null)
            {
                return NotFound();
            }

            _context.CaiGouOrderDetails.Remove(caiGouOrderDetail);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool CaiGouOrderDetailExists(Guid id)
        {
            return _context.CaiGouOrderDetails.Any(e => e.Id == id);
        }
    }
}
